﻿using System.Web.Mvc;
using BookShop.BLL;
using BookShop.DTO;
using BookShop.Library.Common;
using BookShop.Library.Constant;
using PagedList;

namespace BookShop.Admin.Controllers
{
    public class PublisherController : Controller
    {
        public ActionResult Add()
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(string name)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var publisher = PublisherService.Add(new Publisher { Name = name, Status = PublisherStatus.Active });
            if (publisher != null)
            {
                return RedirectToAction("Detail", new { publisherId = publisher.Id });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            ViewBag.AddName = name ?? string.Empty;
            return View();
        }

        public ActionResult Delete(int publisherId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var publisher = PublisherService.GetById(publisherId);
            if (publisher == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            }
            else
            {
                var deleteResult = PublisherService.Delete(publisher);
                if (!deleteResult)
                {
                    TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
                }
            }
            return RedirectToAction("List");
        }

        public ActionResult Detail(int publisherId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var publisher = PublisherService.GetById(publisherId);
            if (publisher != null)
            {
                return View(publisher);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        public ActionResult Edit(int publisherId)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var publisher = PublisherService.GetById(publisherId);
            if (publisher != null)
            {
                return View(publisher);
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
            return RedirectToAction("List");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int publisherId, string name, int status)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherWrite))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("List");
            }
            var publisher = PublisherService.GetById(publisherId);
            if (publisher == null)
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.NotFoundObject;
                return RedirectToAction("List");
            }
            publisher.Name = name;
            publisher.Status = status;
            var updateResult = PublisherService.Update(publisher);
            if (updateResult != null)
            {
                return RedirectToAction("Detail", new { publisherId });
            }
            TempData[AlertMessage.TempDataKey] = AlertMessage.ErrorOccurs;
            return View(publisher);
        }

        [ValidateInput(false)]
        public ActionResult List(int? publisherId, string name, int? status, int page = 1, int pageSize = 20)
        {
            var mod = (Moderator)Session[SessionKey.Moderator];
            if (mod == null)
            {
                return RedirectToAction("Login", "Home");
            }
            if (!CommonFunction.CheckModeratorRole(mod, ModeratorRole.PublisherRead))
            {
                TempData[AlertMessage.TempDataKey] = AlertMessage.UnauthorizedAction;
                return RedirectToAction("Index", "Home");
            }
            int totalRow;
            var model = PublisherService.Search(publisherId, name, status, page, pageSize, out totalRow);
            ViewBag.SearchId = publisherId.ToString();
            ViewBag.SearchName = name ?? string.Empty;
            ViewBag.SearchStatus = status.ToString();
            ViewBag.PageList = new StaticPagedList<Publisher>(model, page, pageSize, totalRow);
            ViewBag.RecordsCount = "Tổng số bản ghi: " + totalRow;
            return View(model);
        }
    }
}
